package loadbalancer;

import java.util.NoSuchElementException;

import base.Element;
import base.LoadBalancer;
import base.Server;
import base.Servers;

/**
 * Send Element in the n + 1 server
 * 
 * @author rweiller
 * 
 */
public class LoadBalancer2 extends LoadBalancer
{
	public int	next	= 0;

	public int getNext()
	{
		return next;
	}

	@Override
	public void load(Element el)
	{
		list.add(el);
	}

	@Override
	public Server send(Servers servers)
	{
		Server sv = null;
		try
		{
			Element el = list.removeFirst();
			servers.add(next, el);
			sv = servers.getList().get(next);
			setNext(servers);
		}
		catch (NoSuchElementException e)
		{
			System.out.println("LoadBalancer2.class -- No Element to pull from loadbalancer");
		}
		return sv;
	}

	/**
	 * Set the next server to receive an Element from the loadbalancer
	 * 
	 * @param servers
	 */
	public void setNext(Servers servers)
	{
		next = (next + 1) % servers.size();
	}

	@Override
	public void sort()
	{}

}